import java.util.*;

public class Main {
    public static void main(String[] args) {
        Page p1 = new Page("111com",0,new Date());
        Page p2 = new Page("222com",0,new Date());

ArrayList<Page> list = new ArrayList<>();
list.add(p1);
list.add(p2);
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("1访问页面");
            System.out.println("2显示访问量排行榜");
            System.out.println("3统计总访问量");
            System.out.println("4计算平均访问量");
            System.out.println("5显示页面最后访问时间");
            System.out.println("6清空访问统计");
            System.out.println("7退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("请输入网页url");
                    String url = sc.next();
                    boolean flag = false;
                    for(int i=0;i<list.size();i++){
                        if(url.equals(list.get(i).getUrl())){
                            int count = list.get(i).getVisits();
                            list.get(i).setVisits(count + 1);
                           list.get(i).setLastVisit(new Date());
                           flag = true;
                        }}
                        if(!flag){
                            Page p3 = new Page(url,1,new Date());
                            list.add(p3);
                        }
                    break;
                case 2:
                        Collections.sort(list, Comparator.comparing(Page::getVisits).reversed());
                      for(int i=0;i<list.size();i++){
                          System.out.println(list.get(i).getUrl());
                      }
                      break;
                case 3:
                    int sum = 0;
                    for(int i=0;i<list.size();i++){
                        sum+=list.get(i).getVisits();
                    }
                    System.out.println(sum);
                    break;
                case 4:
                    int sum2 = 0;
                    for(int i=0;i<list.size();i++){
                        sum2+=list.get(i).getVisits();
                    }
                    double avg = sum2/list.size();
                    System.out.println(avg);
                    break;
                case 5:
                    System.out.println("请输入查询的网页地址");
                    String url1 = sc.next();
                    for(int i=0;i<list.size();i++){
                        if(url1.equals(list.get(i).getUrl())){
                            System.out.println(list.get(i).getLastVisit());
                        }
                    }
                    break;
                case 6:
                    list.clear();
                    break;
            }
            if(choice == 7) {
                break;
            }
        }


    }
}